/*
 * author      : 吴中勤
 * date        : 2018/8/16
 * description : sap相关的一些配置, 但是业务无关
 */

package com.deepexi.adapter.sdk.sap;

import com.deepexi.adapter.common.BaseDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class SapSdkConfig extends BaseDto {

    private static final String PROPERTY_FILE_GLOBLE = "sap.properties";
    private static Logger logger = LoggerFactory.getLogger(SapSdkConfig.class.getName());

    private String baseAuthName;
    private String baseAuthPassword;
    private String hostAddr;

    private String path4BP;
    private String path4Order;
    private String path4Fund;
    private String path4FundFinal;
    private String path4Refund;
    private String path4Callback;

    @Override
    public String toString() {
        return "SapSdkConfig{" +
                "baseAuthName='" + baseAuthName + '\'' +
                ", baseAuthPassword='" + baseAuthPassword + '\'' +
                ", hostAddr='" + hostAddr + '\'' +
                ", path4BP='" + path4BP + '\'' +
                ", path4Order='" + path4Order + '\'' +
                ", path4Fund='" + path4Fund + '\'' +
                ", path4FundFinal='" + path4FundFinal + '\'' +
                ", path4Refund='" + path4Refund + '\'' +
                ", path4Callback='" + path4Callback + '\'' +
                '}';
    }

    public SapSdkConfig() {
    }

    public SapSdkConfig(String baseAuthName, String baseAuthPassword, String hostAddr) {
        this.baseAuthName = baseAuthName;
        this.baseAuthPassword = baseAuthPassword;
        this.hostAddr = hostAddr;
    }

    /**
     * 从配置文件中读取
     *
     * @param profile dev/prod/... 需要找到一个 sap-{profile}
     * @return
     */
    public static SapSdkConfig fromResource(String profile) {
        logger.info("load SapSdkConfig: profile=" + profile);

        String profileConfig = "sap-" + profile + ".properties";

        Properties props = new Properties();
        try {
            // 全局
            InputStream inputStreamGlobal = SapSdkConfig.class
                    .getClassLoader()
                    .getResourceAsStream(PROPERTY_FILE_GLOBLE);
            props.load(inputStreamGlobal);

            // profile
            InputStream inputStreamProfile = SapSdkConfig.class
                    .getClassLoader()
                    .getResourceAsStream(profileConfig);
            props.load(inputStreamProfile);

        } catch (IOException ex) {
            ex.printStackTrace();
            throw new RuntimeException("load property file fail" + ex);
        }

        /** 从 property中读取一个配置 */
        SapSdkConfig sdkConfig = new SapSdkConfig();

        //
        sdkConfig.baseAuthName = props.getProperty("baseAuth.name");
        sdkConfig.baseAuthPassword = props.getProperty("baseAuth.password");
        sdkConfig.hostAddr = props.getProperty("sap.host");

        // path
        sdkConfig.path4BP = props.getProperty("path.BP");
        sdkConfig.path4Order = props.getProperty("path.order");

        sdkConfig.path4Fund = props.getProperty("path.fund");
        sdkConfig.path4FundFinal = props.getProperty("path.fund.final");
        sdkConfig.path4Refund = props.getProperty("path.refund");

        sdkConfig.path4Callback = props.getProperty("path.callback");

        return sdkConfig;
    }

    // getter & setter

    public String getPath4FundFinal() {
        return path4FundFinal;
    }

    public void setPath4FundFinal(String path4FundFinal) {
        this.path4FundFinal = path4FundFinal;
    }

    public String getBaseAuthName() {
        return baseAuthName;
    }

    public void setBaseAuthName(String baseAuthName) {
        this.baseAuthName = baseAuthName;
    }

    public String getBaseAuthPassword() {
        return baseAuthPassword;
    }

    public void setBaseAuthPassword(String baseAuthPassword) {
        this.baseAuthPassword = baseAuthPassword;
    }

    public String getHostAddr() {
        return hostAddr;
    }

    public void setHostAddr(String hostAddr) {
        this.hostAddr = hostAddr;
    }

    public String getPath4BP() {
        return path4BP;
    }

    public void setPath4BP(String path4BP) {
        this.path4BP = path4BP;
    }

    public String getPath4Order() {
        return path4Order;
    }

    public void setPath4Order(String path4Order) {
        this.path4Order = path4Order;
    }

    public String getPath4Fund() {
        return path4Fund;
    }

    public void setPath4Fund(String path4Fund) {
        this.path4Fund = path4Fund;
    }

    public String getPath4Callback() {
        return path4Callback;
    }

    public void setPath4Callback(String path4Callback) {
        this.path4Callback = path4Callback;
    }

    public String getPath4Refund() {
        return path4Refund;
    }

    public void setPath4Refund(String path4Refund) {
        this.path4Refund = path4Refund;
    }
}
